package com.lut.web;

import com.google.gson.Gson;
import com.lut.pojo.Book;
import com.lut.pojo.Cart;
import com.lut.pojo.CartItem;
import com.lut.service.BookService;
import com.lut.service.impl.BookServiceImpl;
import com.lut.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @user:dell
 * @date:2020年10月15 20:13
 */
public class CartServlet  extends BaseServlet{
    private BookService bookService=new BookServiceImpl();
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //      获取请求的商品编号
        int id = WebUtils.ParseInt(req.getParameter("id"), 0);
//        调用bookservice的querybookbyid查询图书信息
        Book book = bookService.queryBookById(id);
//        把图书信息转换称为cartitem项
        CartItem cartItem = new CartItem(book.getId(), 1, book.getName(), book.getPrice(), book.getPrice());
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart==null){
            cart=new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
//        重定向回原来商品所在的地址
        resp.sendRedirect(req.getHeader("Referer"));
        req.getSession().setAttribute("lastName",cartItem.getName());

    }
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//      获取商品编号
       int id = WebUtils.ParseInt(req.getParameter("id"), 0);
//       获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
//            删除购物车商品项
            cart.deleteItem(id);
//        重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取购物车对象
       Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clear();
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         Cart cart= (Cart) req.getSession().getAttribute("cart");
        int id = WebUtils.ParseInt(req.getParameter("id"), 0);
        int count = WebUtils.ParseInt(req.getParameter("count"), 1);
        if(cart!=null){
             cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));
         }
    }
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //      获取请求的商品编号
        int id = WebUtils.ParseInt(req.getParameter("id"), 0);
//        调用bookservice的querybookbyid查询图书信息
        Book book = bookService.queryBookById(id);
//        把图书信息转换称为cartitem项
        CartItem cartItem = new CartItem(book.getId(), 1, book.getName(), book.getPrice(), book.getPrice());
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if(cart==null){
            cart=new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        Map<String,Object>resultMap=new HashMap<>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());
        Gson gson=new Gson();
        String result = gson.toJson(resultMap);
        resp.getWriter().write(result);
    }
}
